package com.xms.datacore.service;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xms.core.exception.XmsException;
import com.xms.datacore.TransactionUtil;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.Collection;

/**
 * <p>
 * CreaterService
 * </p>
 *
 * @author migo
 * @since 2021/9/2
 */
public class CreaterService<T> extends ServiceImpl<BaseMapper<T>, T> implements ICreaterService<T> {

    @Override
    public IService<T> getService() {
        return this;
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean create(T entity) throws SQLException, XmsException {
        return super.save(entity);
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public boolean createMany(Collection<T> entityList) throws SQLException, XmsException {
        for (T entity : entityList){
            this.create(entity);
        }
        return true;
        //return super.saveBatch(entityList, batchSize);
    }

    @Override
    public void rollBack(){
        TransactionUtil.rollBack();
    }

    /**
     * 覆写mybatis-plus获取泛型class方式，防止多重继承时无法获取class
     */
    /*@SuppressWarnings("all")
    @Override
    protected Class<T> currentModelClass() {
        Object cls = getClass();
        return (Class<T>) GenericTypeResolver.resolveTypeArgument(getClass(), CreaterService.class);
    }*/
}
